package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.ZslCar;
import com.cz.zslu.entity.ZslStore;
import com.cz.zslu.entity.ZslStoreCommodity;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.model.Car;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IZslCarService;
import com.cz.zslu.service.IZslStoreCommodityService;
import com.cz.zslu.service.IZslStoreService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-10-25
 */
@RestController
@RequestMapping("/sys/car")
@Api(description = "购物车管理")
public class CarController {
    @Autowired
    private IZslStoreCommodityService iZslStoreCommodityService;
    @Autowired
    private IZslCarService iZslCarService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IZslStoreService iZslStoreService;

    @ApiOperation("添加到购物车")
    @RequestMapping(value = "/addCar", method = RequestMethod.POST)
    public ReturnObject addCar(@ApiParam("商品id") @RequestParam Long commodityId,
                               @ApiParam("数量") @RequestParam Integer count) {
        ZslStoreCommodity zslStoreCommodity = iZslStoreCommodityService.selectById(commodityId);
        ZslCar zslCar = iZslCarService.selectOne(new EntityWrapper<ZslCar>().eq("info.user_id",
                BaseContextHandler.getUserID()).eq("info.commodity_id", commodityId));
        if (zslCar != null) {
            zslCar.setCount(zslCar.getCount() + count);
        } else {
            zslCar = new ZslCar(BaseContextHandler.getUserID(), commodityId, zslStoreCommodity.getUserId(), count, null,
                    null, null, null, null);
        }
        if (zslCar.getId() == null) {
            iZslCarService.insert(zslCar);
        } else {
            iZslCarService.updateById(zslCar);
        }

        return new ReturnObject<>(0, "购物车添加成功");
    }


    @ApiOperation("获取购物车列表")
    @RequestMapping(value = "/getCarList", method = RequestMethod.POST)
    public ReturnObject<List<Car>> getCarList() {
        List<Car> cars = Lists.newArrayList();
        EntityWrapper<ZslCar> ew = new EntityWrapper<>();
        ew.eq("info.user_id", BaseContextHandler.getUserID());
        ew.groupBy("info.seller_user_id");
        ew.orderBy("info.crt_time", false);
        List<ZslCar> list = iZslCarService.selectList(ew);
        for (ZslCar info : list) {
            ZslStore zslStore =
                    iZslStoreService.selectById(iSysUserService.selectById(info.getSellerUserId()).getStoreId());
            cars.add(new Car(zslStore.getName(), info.getType(), info.getSellerUserId(),
                    zslStore.getId(), iZslCarService.selectList(new EntityWrapper<ZslCar>().eq("info.user_id",
                    BaseContextHandler.getUserID()).eq("info.seller_user_id", info.getSellerUserId()))));
        }
        return new ReturnObject<>(0, "获取成功", cars);
    }


    @ApiOperation("删除购物车商品")
    @RequestMapping(value = "/delCar", method = RequestMethod.POST)
    public ReturnObject delCar(@ApiParam("商品id") @RequestParam Long commodityId) {
        iZslCarService.delete(new EntityWrapper<ZslCar>().eq("commodity_id", commodityId).eq("user_id",
                BaseContextHandler.getUserID()));
        return new ReturnObject<>(0, "删除成功");
    }

    @ApiOperation("修改购物车商品数量")
    @RequestMapping(value = "/editCar", method = RequestMethod.POST)
    public ReturnObject editCar(@ApiParam("商品id") @RequestParam Long commodityId,
                                @ApiParam("商品数量") @RequestParam Integer count) {
        ZslCar zslCar = new ZslCar();
        zslCar.setCount(count);
        iZslCarService.update(zslCar, new EntityWrapper<ZslCar>().eq("commodity_id", commodityId).eq("user_id",
                BaseContextHandler.getUserID()));
        return new ReturnObject<>(0, "修改成功");
    }


}

